package org.javaweb.cms_web.controller;

import org.javaweb.cms_web.model.FeedbackDTO;
import org.javaweb.cms_web.mapper.FeedbackDTOMapper;

import org.javaweb.cms_web.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

//接收问题提交并保存到数据库
@Controller
public class SubmitFeedbackController {

    @Autowired
    private FeedbackDTOMapper feedbackDTOMapper;
    @Resource
    private HostHolder hostHolder;

    @PostMapping("/SubmitFeedbackController")
    public String submitFeedback(@RequestParam("title") String title,
                                 @RequestParam("description") String description,
                                 HttpSession session) {

        // 从 session 中获取用户 ID
        Integer userId = hostHolder.getUser().getUserId();
        if (userId == null) {
            return "redirect:login.jsp";
        }

        // 创建 Feedback 实体
        FeedbackDTO feedbackDTO = new FeedbackDTO();
        feedbackDTO.setTitle(title);
        feedbackDTO.setDescription(description);
        feedbackDTO.setUser_id(userId);
        feedbackDTO.setStatus(0); // 默认状态为未处理

        // 插入到数据库
        feedbackDTOMapper.insertFeedback(feedbackDTO);

        // 返回到互动页面，提示成功信息
        session.setAttribute("message", "您的问题已提交，等待管理员回复。");
        return "redirect:interactiveFeature.jsp";
    }
}
